# 1. 导入 AndroidBotMain 类
from PyAibote import AndroidBotMain
from PyAibote import WinBotMain
import time

class Hid():
    """Hid类, 模拟键鼠操作"""
    def __init__(self, bot : AndroidBotMain):
        self.bot = bot

    



class CustomAndroidScript(AndroidBotMain):
    # 2.1. 设置是否终端打印输出 DEBUG：输出， INFO：不输出, 默认打印输出
    Log_Level = "DEBUG" 

    # 2.2. 终端打印信息是否存储LOG文件 True： 储存， False：不存储
    Log_Storage = True

    hid_host = "http://127.0.0.1:9123"

    # 添加类属性
    char_to_keycode = {
        'a': 4, 'b': 5, 'c': 6, 'd': 7, 'e': 8, 'f': 9, 
        'g': 10, 'h': 11, 'i': 12, 'j': 13, 'k': 14, 'l': 15,
        'm': 16, 'n': 17, 'o': 18, 'p': 19, 'q': 20, 'r': 21, 
        's': 22, 't': 23, 'u': 24, 'v': 25, 'w': 26, 'x': 27,
        'y': 28, 'z': 29,
        '1': 30, '2': 31, '3': 32, '4': 33, '5': 34, 
        '6': 35, '7': 36, '8': 37, '9': 38, '0': 39,
        '\n': 40,  # ENTER
        '\t': 43,  # TAB
        ' ': 44,   # SPACE
        '-': 45,   # - and _
        '=': 46,   # = and +
        '[': 47,   # [ and {
        ']': 48,   # ] and }
        '\\': 49,  # \ and |
        ';': 51,   # ; and :
        "'": 52,   # ' and "
        '`': 53,   # ` and ~
        ',': 54,   # , and <
        '.': 55,   # . and >
        '/': 56    # / and ?
    }

    def hid_getBatteryLevel(self):
        """获取手机电量"""
        ret = self.url_request(f"{self.hid_host}/getBatteryLevel")
        print(ret)
        
    def hid_click(self, x, y):
        """点击坐标"""
        res = self.url_request(f"{self.hid_host}/click?x={int(x)}&y={int(y)}")
        print(res)

    def hid_home(self):
        """主页"""
        res = self.url_request(f"{self.hid_host}/home")
        print(res)

    def hid_back(self):
        """返回"""
        res = self.url_request(f"{self.hid_host}/back")
        print(res)
    
    def hid_key(self, modify : int, keyCode : int):
        """按键"""
        res = self.url_request(f"{self.hid_host}/keyPress?modify={modify}&keyCode={keyCode}")
        print(res)

    def hid_input(self, text: str):
        """输入文本
        Args:
            text (str): 要输入的文本
        """

        for char in text:
            char = char.lower()  # 转换为小写
            if char in self.char_to_keycode:
                # 普通字符，直接发送键码
                self.hid_key(0, self.char_to_keycode[char])
            elif char.isupper():
                # 大写字符，需要按住Shift (modify=1)
                self.hid_key(1, self.char_to_keycode[char.lower()])
            else:
                # 不支持的字符，跳过
                continue
            time.sleep(0.05)  # 添加短暂延迟，确保按键事件能被正确处理

    def hid_selectAll(self):
        """全选"""
        res = self.url_request(f"{self.hid_host}/selectAll")
        print(res)

    def hid_backspace(self, frequency : int = 1):
        """退格"""
        res = self.url_request(f"{self.hid_host}/backspace?frequency={frequency}")
        print(res)

    def hid_click_text(self, text):
        """
        OCR 点击文本
        """
        result = self.find_text(text)
        print(result)
        if result:
            self.hid_click(result[0], result[1])

    # 2.3. 注意：script_main 此方法是脚本执行入口必须存在此方法
    def script_main(self):
        result = self.init_ocr_server("127.0.0.1", False, True, True) 


        self.start_app("淘宝", 5, 0.5)
        time.sleep(1)

        # self.hid_click(559.3333333333334, 1800.0)

        self.hid_click_text("同意")

        time.sleep(1)

        self.hid_click_text("我的淘宝")



if __name__ == '__main__':
    CustomAndroidScript.execute("0.0.0.0", 8888)